<?php
class YoukuTv
{
	public $ShowPageUrl = '';
	public $ShowPageId = '';
	public $TvName = '';
	public $AjaxPageCount = 0;
	public $Path = array();
	public $VShow = array();
	public $Rs = array();
	public $YoukuDomain = 'http://www.youku.com/';

	public function __construct($ShowPageUrl, $TvName, $Path = '')
	{
		$this->ShowPageUrl = $ShowPageUrl;
		if(preg_match('/id_([0-9a-zA-Z]+)\.html$/', $ShowPageUrl, $Matches))
		{
			$this->ShowPageId = $Matches[1];
		}
		$this->TvName = $TvName;
		$this->Path['root'] = $Path ? $Path : dirname(__FILE__)."/tv/";
		$this->makeDir();
	}

	public function makeDir()
	{
		if($this->Path['root'])
		{
			$this->Path['html'] = $this->Path['root']."downloadfile/html/".$this->TvName."/";
			if(!file_exists($this->Path['html']))
			{
				mkdir($this->Path['html'], "0777", true);
			}
			$this->Path['actor'] = $this->Path['root']."uploadfile/actor/";
			if(!file_exists($this->Path['actor']))
			{
				mkdir($this->Path['actor'], "0777", true);
			}
			$this->Path['tv'] = $this->Path['root']."uploadfile/tv/".$this->TvName."/";
			if(!file_exists($this->Path['tv']))
			{
				mkdir($this->Path['tv'], "0777", true);
			}
		}
	}

	public function getPage($Url, $FileName = "")
	{
		$ch = curl_init();
		if(empty($FileName))
		{
			preg_match('/(id_[0-9a-zA-Z]+\.html)$/', $Url, $Matches);
			$FileName = $this->Path['html'].$Matches[1];
		}
		else
		{
			$FileName = $this->Path['html'].$FileName;
		}
		if(!file_exists($FileName))
		{
			$fp = fopen($FileName, "w");

			curl_setopt($ch, CURLOPT_URL, $Url);
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_setopt($ch, CURLOPT_HEADER, 0);

			curl_exec($ch);
			curl_close($ch);
			fclose($fp);
		}
		return $FileName;
	}

	public function getShowPage()
	{
		return $this->getPage($this->ShowPageUrl, 'show_page_'.$this->ShowPageId.'.html');
	}

	public function getAjaxListPage()
	{
		$Page = 1;
		$this->getPage($this->YoukuDomain.'show_eplist/showid_'.$this->ShowPageId.'_type_list_from_ajax_page_'.$Page.'.html', 'ajax_list_'.$Page.'_'.$this->ShowPageId.'.html');
		$Content = file_get_contents($this->Path['html'].'ajax_list_'.$Page.'_'.$this->ShowPageId.'.html');
		preg_match('/<li class="last" title="最后"><a[^>]*><em class="ico_next"><\/em>([\d]+)<\/a><\/li>/', $Content, $Matches);

		if(isset($Matches[1]) && $Matches[1] > 1)
		{
			while($Page < $Matches[1])
			{
				$Page += 1;
				$this->getPage($this->YoukuDomain.'show_eplist/showid_'.$this->ShowPageId.'_type_list_from_ajax_page_'.$Page.'.html', 'ajax_list_'.$Page.'_'.$this->ShowPageId.'.html');
			}
		}
		$this->AjaxPageCount = $Page;
	}

	public function getActorAvatar($Url, $FileName = "", $Type = "jpg")
	{
		$ch = curl_init();
		if(empty($FileName))
		{
			preg_match('/\/([-0-9a-zA-Z]+)$/', $Url, $Matches);
			$FileName = $this->Path['actor'].$Matches[1].".".$Type;
		}
		else
		{
			$FileName = $this->Path['actor'].iconv("UTF-8", "GBK", $FileName).".".$Type;
		}
		if(!file_exists($FileName))
		{
			$fp = fopen($FileName, "w");

			curl_setopt($ch, CURLOPT_URL, $Url);
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_setopt($ch, CURLOPT_HEADER, 0);

			curl_exec($ch);
			curl_close($ch);
			fclose($fp);
		}
		return $FileName;
	}

	public function getTvPic($Url, $FileName = "", $Type = "jpg")
	{
		$ch = curl_init();
		if(empty($FileName))
		{
			preg_match('/\/([-0-9a-zA-Z]+)$/', $Url, $Matches);
			$FileName = $this->Path['tv'].$Matches[1].".".$Type;
		}
		else
		{
			$FileName = $this->Path['tv'].iconv("UTF-8", "GBK", $FileName).".".$Type;
		}
		if(!file_exists($FileName))
		{
			$fp = fopen($FileName, "w");

			curl_setopt($ch, CURLOPT_URL, $Url);
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_setopt($ch, CURLOPT_HEADER, 0);

			curl_exec($ch);
			curl_close($ch);
			fclose($fp);
		}
		return $FileName;
	}

	public function parseShowPage()
	{
		$Content = file_get_contents($this->Path['html'].'show_page_'.$this->ShowPageId.'.html');
		$Title = substr($Content, strpos($Content, '<title>') + 7, strpos($Content, '</title>')-strpos($Content, '<title>')-7);
		$this->Rs['title'] = substr($Title, 0, strpos($Title, ' -'));
		$Year = substr($Content, strpos($Content, '<span class="pub">(')+19, 4);
		$this->Rs['year'] = $Year;
		$Type = substr($Content, strpos($Content, '<ul class="baseinfo">')+21, strpos($Content, '<span id="infoShort" class="short">')-(strpos($Content, '<ul class="baseinfo">')+21));
		preg_match_all('/<a target="_blank"[^>]*>([^<]*)<\/a>/', $Type, $Matches);
		if(isset($Matches[1]) && is_array($Matches[1]))
		{
			$this->Rs['area'] = $Matches[1][0];
			$TypeStr = implode(',', $Matches[1]);
			$this->Rs['type'] = substr($TypeStr, strpos($TypeStr, ',')+1);
		}
		preg_match('/<em class="num">([^<]*)<\/em>/', $Content, $Matches);
		if(isset($Matches[1]))
		{
			$this->Rs['rate'] = $Matches[1];
		}
		preg_match('/<span id="infoShort"\s+class="short">([\w\W]*?)<\/span>/', $Content, $Matches);
		if(isset($Matches[1]))
		{
			$this->Rs['contentshort'] = $Matches[1];
		}
		preg_match('/<span id="infoLong" style="display:none">([\w\W]*?)<\/span>/', $Content, $Matches);
		if(isset($Matches[1]))
		{
			$this->Rs['contentlong'] = $Matches[1];
		}
		preg_match_all('/<li class="name"><a charset="[^>]*>([\w\W]*?)<\/a><\/li>/', $Content, $Matches);
		if(isset($Matches[1]))
		{
			$this->Rs['actor'] = $Matches[1];
		}
		preg_match_all('/<li class="role" title="[^>]*>([\w\W]*?)<\/li>/', $Content, $Matches);
		if(isset($Matches[1]))
		{
			$this->Rs['role'] = $Matches[1];
		}
		preg_match_all('/<li class="photo"><a[^>]*><img src="([^"]*?)" alt="([\w\W]*?)"><\/a><\/li>/', $Content, $Matches);
		if(isset($Matches[1]) && is_array($Matches[1]))
		{
			foreach($Matches[1] as $Key => $Val)
			{
				$this->getActorAvatar($Val, $Matches[2][$Key]);
			}
		}
		preg_match_all('/<a charset="411-13-1-[\d]+" [^>]*>([\w\W]*?)<\/a>/', $Content, $Matches);
		if(isset($Matches[1]) && is_array($Matches[1]))
		{
			$this->Rs['director'] = implode(',', $Matches[1]);
		}
		preg_match_all('/<a charset="411-13-4-[\d]+" [^>]*>([\w\W]*?)<\/a>/', $Content, $Matches);
		if(isset($Matches[1]) && is_array($Matches[1]))
		{
			$this->Rs['playwright'] = implode(',', $Matches[1]);
		}
		preg_match_all('/<a charset="411-13-5-[\d]+" [^>]*>([\w\W]*?)<\/a>/', $Content, $Matches);
		if(isset($Matches[1]) && is_array($Matches[1]))
		{
			$this->Rs['manager'] = implode(',', $Matches[1]);
		}
		unset($Content);
	}

	public function parseAjaxListPage()
	{
		if($this->AjaxPageCount > 0)
		{
			$Page = 1;
			while($Page <= $this->AjaxPageCount)
			{
				$Content = file_get_contents($this->Path['html'].'ajax_list_'.$Page.'_'.$this->ShowPageId.'.html');
				preg_match_all('/<li class="v_thumb"><img src="([^"]*)" alt="([^"]*)"[\w\W]*?\/><\/li>/', $Content, $Matches);
				if(isset($Matches[1]) && is_array($Matches[1]))
				{
					foreach($Matches[1] as $Key => $Val)
					{
						$this->Rs['subtitle'][] = $Matches[2][$Key];
						$this->getTvPic($Val, $Matches[2][$Key]);
					}
				}
				preg_match_all('/<li class="v_link"><a charset="411-4-3-[\d]+"[\w\W]*?href="([^"]*)"[\w\W]*?><\/a><\/li>/', $Content, $Matches);
				if(isset($Matches[1]) && is_array($Matches[1]))
				{
					foreach($Matches[1] as $Key => $Val)
					{
						$Id = substr($Val, strpos($Val, 'id_')+3, strpos($Val, '.html')-strpos($Val, 'id_')-3);
						$this->VShow[] = $Id;
						$this->getPage($Val, 'v_show_'.$Id.'.html');
					}
				}
				preg_match_all('/<div class="v_desc">([\w\W]*?)<\/div>/', $Content, $Matches1);
				preg_match_all('/<div class="v_desc" style="display:none;">([\w\W]*?)<\/div>/', $Content, $Matches2);
				if(isset($Matches1[1]) && is_array($Matches1[1]))
				{
					if(isset($Matches2[1]) && is_array($Matches2[1]) && count($Matches2[1]) == count($Matches1[1]))
					{
						foreach($Matches1[1] as $Key => $Val)
						{
							$this->Rs['subcontentshort'][] = $Val;
							$this->Rs['subcontentlong'][] = $Matches2[1][$Key];
						}
					}
					else
					{
						foreach($Matches1[1] as $Key => $Val)
						{
							$this->Rs['subcontentshort'][] = $Val;
							$Find = 0;
							foreach($Matches2[1] as $K=> $V)
							{
								if(strstr($V, substr($Val, 0, strpos($Val, '...'))) !== false)
								{
									$Find = 1;
									$this->Rs['subcontentlong'][] = $V;
									unset($Matches2[1][$K]);
									break;
								}
							}
							if($Find == 0)
							{
								$this->Rs['subcontentlong'][] = $Val;
							}
						}
					}
				}
				$Page += 1;
				unset($Content);
			}
		}
	}

	public function parseVShowPage()
	{
		if(is_array($this->VShow) && $this->VShow)
		{
			foreach($this->VShow as $Key => $Val)
			{
				$Content = file_get_contents($this->Path['html'].'v_show_'.$Val.'.html');
				preg_match('/<input type="text" id="link2" value="([^"]*)" \/>/', $Content, $Matches);
				if(isset($Matches[1]) && $Matches[1])
				{
					$this->Rs['flash'][] = $Matches[1];
				}
			}
		}
	}

	public function getAndParse()
	{
		$this->getShowPage();
		$this->parseShowPage();
		$this->getAjaxListPage();
		$this->parseAjaxListPage();
		$this->parseVShowPage();
		
		return $this->Rs;
	}
}


?>